package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 13545
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/11/30 21:12
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileUploadController {

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.substring(file.getOriginalFilename(),
                        StringUtils.lastIndexOf(file.getOriginalFilename(),".")+1,
                        StringUtils.length(file.getOriginalFilename())));
        //调用FastDFSUtil工具类将文件传入到FastDFS中
        String[] uploads = FastDFSUtil.upload(fastDFSFile);
        //组装文件上传地址
        String filePath = FastDFSUtil.getTrackerUrl()+"/"+uploads[0]+"/"+uploads[1];
        return new Result(true, StatusCode.OK,"上传成功！",filePath);
    }
}
